//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <string.h>

int __cdecl _strnicmp(const char* str1, const char* str2, size_t count)
{
    int c1, c2;
    if (count) {
        do {
            c1 = *str1++;
            if (c1 >= 'A' && c1 <= 'Z') // convert to lower case.
                c1 -= 'A' - 'a';

            c2 = *str2++;
            if (c2 >= 'A' && c2 <= 'Z') // convert to lower case.
                c2 -= 'A' - 'a';
        } while (--count && c1 && (c1 == c2));
        return (c1 - c2);
    }
    return 0;

/*
    register int __res;
#if !defined(_arm)
    int d0, d1;
    asm __volatile__(
        "cmp $1, %5\n\t"
        "je  3f\n\t"
        "movl %6, %%edi\n"
        "1:\tdecl %5\n\t"
        "js 4f\n\t"
        "movl (%%esi), %%ebx\n\t"
        "cmpb $0x41, %%bl\n\t" //convert to lower case.
        "jb 2f\n\t"
        "cmpb $0x5a, %%bl\n\t"
        "ja 2f\n\t"
        "addb $0x20, %%bl\n\t"
        "2:\tmovl (%%edi), %%edx\n\t"
        "cmpb $0x41, %%dl\n\t" //convert to lower case.
        "jb 3f\n\t"
        "cmpb $0x5a, %%dl\n\t"
        "ja 3f\n\t"
        "addb $0x20, %%dl\n\t"
        "3:\tsub %%dx, %%bx\n\t"
        "cmpb $0, %%bl\n\t"
        "jne 4f\n\t"
        "incl %%edi\n\t"
        "incl %%esi\n\t"
        "cmpl $0, (%%edi)\n\t"
        "jne 1b\n"
        "4:\txorl %%eax, %%eax\n\t"
        "movw %%bx, %%ax\n\t"
        :"=a"(__res), "=&S" (d0), "=&c"(d1)
        :"0" (0), "1" (str1), "2"(count + 1), "g" (str2)
        :"di", "bx");
#else
    asm __volatile__(
        "cmp r2, #0\n\t"
        "beq 5f\n\t"
        "1:\tldr r3, [r0], #1\n\t"
        "cmp r3, #0x41\n\t"
        "blt 2f\n\t"
        "cmp r3, #0x5a\n\t"
        "bhi 2f\n\t"
        "sub r3, r3, #0x20\n"
        "2:\tldr r4, [r1], #1\n\t"
        "cmp r4, #0x41\n\t"
        "blt 3f\n\t"
        "cmp r4, #0x5a\n\t"
        "bhi 3f\n\t"
        "sub r4, r4, #0x20\n"
        "3:\tsub r2, r2, #1\n\t"
        "cmp r3, r4\n\t"
        "bne 4f\n\t"
        "cmp r2, #0\n\t"
        "ble 4f\n\t"
        "cmp r4, #0\n\t"
        "bne 1b\n"
        "4:\tsub r5, r3, r4\n\t"
        "mov %0, r5\n"
        "b 6f\n"
        "5:\tmov %0, #0\n"
        "6:\n"
        :"=r"(__res)
        ::"r5", "r3", "r4");
#endif

    return __res;
*/
}

int __cdecl _wcsnicmp(const wchar_t* str1, const wchar_t* str2, size_t count)
{
    int c1, c2;
    if (count) {
        do {
            c1 = *str1++;
            if (c1 >= 'A' && c1 <= 'Z') // convert to lower case.
                c1 -= 'A' - 'a';

            c2 = *str2++;
            if (c2 >= 'A' && c2 <= 'Z') // convert to lower case.
                c2 -= 'A' - 'a';
        } while (--count && c1 && (c1 == c2));
        return (c1 - c2);
    }
    return 0;
/*
    register int __res;
#if !defined(_arm)
    int d0, d1;
    asm __volatile__(
        "cmp $1, %5\n\t"
        "je  3f\n\t"
        "movl %6, %%edi\n"
        "1:\tdecl %5\n\t"
        "js 4f\n\t"
        "movl (%%esi), %%ebx\n\t"
        "cmpw $0x41, %%bx\n\t" //convert to lower case.
        "jb 2f\n\t"
        "cmpw $0x5a, %%bx\n\t"
        "ja 2f\n\t"
        "addw $0x20, %%bx\n\t"
        "2:\tmovl (%%edi), %%edx\n\t"
        "cmpw $0x41, %%dx\n\t" //convert to lower case.
        "jb 3f\n\t"
        "cmpw $0x5a, %%dx\n\t"
        "ja 3f\n\t"
        "addw $0x20, %%dx\n\t"
        "3:\tsubw %%dx, %%bx\n\t"
        "cmpw $0, %%bx\n\t"
        "jne 4f\n\t"
        "addl $2, %%edi\n\t"
        "addl $2, %%esi\n\t"
        "cmpl $0, (%%edi)\n\t"
        "jne 1b\n"
        "4:\txorl %%eax, %%eax\n\t"
        "mov %%bl, %%al\n\t"
        :"=a"(__res), "=&S" (d0), "=&c"(d1)
        :"0" (0), "1" (str1), "2"(count + 1), "g" (str2)
        :"di", "bx");
#else
    asm __volatile__(
        "cmp r2, #0\n\t"
        "beq 5f\n"
        "1:\tldrh r3, [r0], #2\n\t"
        "cmp r3, #0x41\n\t"
        "blt 2f\n\t"
        "cmp r3, #0x5a\n\t"
        "bhi 2f\n\t"
        "add r3, r3, #0x20\n"
        "2:\tldrh r4, [r1], #2\n\t"
        "cmp r4, #0x41\n\t"
        "blt 3f\n\t"
        "cmp r4, #0x5a\n\t"
        "bhi 3f\n\t"
        "add r4, r4, #0x20\n"
        "3:\tsub r2, r2, #2\n\t"
        "cmp r4, r3\n\t"
        "bne 4f\n\t"
        "sub r2, r2, #1\n\t"
        "cmp r2, #0\n\t"
        "ble 4f\n\t"
        "mov r4, r4, lsl #16\n\t"
        "mov r4, r4, lsr #16\n\t"
        "cmp r4, #0\n\t"
        "bne 1b\n"
        "4:\tsub r5, r3, r4\n\t"
        "mov %0, r5\n"
        "b 6f\n"
        "5:\tmov %0, #0\n"
        "6:\n"
        :"=r"(__res)
        ::"r5", "r3", "r4");
#endif

    return __res;
*/
}
